﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace CoursePack
{
    [Serializable]
    public class CourseSequence
    {
        #region Data
        private List<SuggestedSemester> m_Years;
        #endregion


        public CourseSequence(string _Path)
        {
            this.m_Years = new List<SuggestedSemester>();

            this.GetCourseSequence(_Path);

        }

        private void GetCourseSequence(string _Path)
        {
            #region Loading CourseSequence
            System.IO.StreamReader sequence = new System.IO.StreamReader(_Path);

            string line, year = "";
            while ((line = sequence.ReadLine()) != null)
            {

                // New Year
                if (line.Contains("###"))
                {
                    //Get the year
                    line = sequence.ReadLine();
                    year = Regex.Match(line, @"\d+").Value;

                    //Get the session
                    line = sequence.ReadLine();
                    ESession esession = (ESession)Enum.Parse(typeof(ESession), line);
                    Session session = new Session(int.Parse(year), esession);

                    SuggestedSemester semester = new SuggestedSemester(session);

                    this.AddSemester(semester);
                    line = sequence.ReadLine();


                    // We take the courses
                    while (!line.Contains('#') && !line.Contains('*') && line != null)
                    {
                        string[] tab = line.Split('|');

                        string cN = (tab[0].Split(' ').Length > 1) ? tab[0].Split(' ')[0] : "";

                        string cNum = (tab[0].Split(' ').Length > 1) ? tab[0].Split(' ')[1] : "";
                        string cc = tab.Length > 1 ? tab[1] : "";
                        string credits = tab.Length > 2 ? tab[2] : "0.0";

                        semester.AddCourse(new CourseDescription(cN, cNum, cc, credits, "", "", CourseStatus.NotStarted));
                        line = sequence.ReadLine();
                    }

                }
                else if (line.Contains(ESession.Winter.ToString()) || line.Contains(ESession.Summer.ToString()) || line.Contains(ESession.Fall.ToString()))
                {
                    //Get the session
                    ESession esession = (ESession)Enum.Parse(typeof(ESession), line);
                    Session session = new Session(int.Parse(year), esession);

                    SuggestedSemester semester = new SuggestedSemester(session);

                    this.AddSemester(semester);

                    line = sequence.ReadLine();


                    // We take the courses
                    while (!line.Contains('#') && !line.Contains('*') && line != null)
                    {
                        string[] tab = line.Split('|');

                        string cN = (tab[0].Split(' ').Length > 1) ? tab[0].Split(' ')[0] : "";

                        string cNum = (tab[0].Split(' ').Length > 1) ? tab[0].Split(' ')[1] : "";
                        string cc = tab.Length > 1 ? tab[1] : "";
                        string credits = tab.Length > 2 ? tab[2] : "0.0";

                        semester.AddCourse(new CourseDescription(cN, cNum, cc, credits, "", "", CourseStatus.NotStarted));
                        line = sequence.ReadLine();
                    }
                }
            }

            #endregion
        }

        public void AddSemester(SuggestedSemester _Semester)
        {
            this.m_Years.Add(_Semester);
        }

        public List<CourseDescription> CourseInSemester(int _year, ESession _session)
        {

            foreach (SuggestedSemester sem in this.m_Years)
            {
                if (sem.Session.ESession == _session && sem.Session.Year == _year)
                    return sem.SuggestedCourses;
            }

            return null;
        }

        #region Accessors

        #endregion
    }
}
